package com.team.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.dao.AdminMapper;
import com.team.dao.ReservationMapper;
import com.team.dao.SectionMapper;
import com.team.easyexcel.DiseaseExcel;
import com.team.entity.Admin;
import com.team.entity.Reservation;
import com.team.entity.Section;
import com.team.service.ReservationService;
import com.team.util.WebUtil;
import com.team.vo.ReservationVo;
import com.team.vo.Result;
import com.team.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 陈灰灰
 * @Date: 2022/10/15/16:25
 * @Description:
 */
@Service
public class ReservationServiceImpl implements ReservationService {
    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private SectionMapper sectionMapper;
    @Override
    public Result queryAllReservationsByAccount(int currentPage,int pageSize,String account,ReservationVo reservationVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<Reservation> reservationList = reservationMapper.queryAllReservationsByAccount(account,reservationVo.getName(),reservationVo.getVisit_time());
        List<Section> sections = new ArrayList<>();
        for (Reservation reservation : reservationList) {
            Map<String,Object> map = new HashMap<>();
            Section section = sectionMapper.selectByPrimaryKey(reservation.getDuty().getSectionId());
            sections.add(section);
        }
        PageInfo<Reservation> info = new PageInfo<>(reservationList);
        Map<String,Object> data = new HashMap<>();
        data.put("info",info);
        data.put("sections",sections);

        return new Result(200,"查询成功",data);
    }

    @Override
    public Result deleteById(int id) {
        reservationMapper.deleteByPrimaryKey(id);
        return new Result(200,"删除成功");
    }

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Result toCancel(UserVo userVo) {
        List<Reservation> reservations = reservationMapper.queryAllByIdcardAndNameAndTel(userVo);
        if(reservations != null){
            List<Map<String,Object>> data = new ArrayList<>();
            for (Reservation reservation : reservations) {
                HashMap<String, Object> m = new HashMap<>();
                Admin admin = adminMapper.selectByAccount(reservation.getDuty().getAccount());
                Section section = sectionMapper.selectByPrimaryKey(reservation.getSid());
                m.put("accountName",admin.getName());
                m.put("sectionName",section.getName());
                m.put("reservation",reservation);
                data.add(m);
            }
            WebUtil.getSession().setAttribute("user",userVo);
            return new Result(200, "查询成功", data);
        }
        return new Result(500,"查询失败");
    }

    @Override
    public Result cancel(Integer id) {
        int i = reservationMapper.updateIsdalete(id);
        return new Result(i==0?500:200,i==0?"取消预约失败":"取消预约成功");
    }

    @Override
    public Result quitCancel() {
        WebUtil.getSession().removeAttribute("user");
        return new Result(200,"退出成功");
    }

    @Override
    public Result queryByNameAndIDCard(UserVo userVo) {
        List<Reservation> reservations = reservationMapper.queryByNameAndIDcard(userVo.getName(),userVo.getIdcard());
        if(reservations != null){
            List<Map<String,Object>> data = new ArrayList<>();
            for (Reservation reservation : reservations) {
                HashMap<String, Object> m = new HashMap<>();
                Admin admin = adminMapper.selectByAccount(reservation.getDuty().getAccount());
                Section section = sectionMapper.selectByPrimaryKey(reservation.getSid());
                m.put("accountName",admin.getName());
                m.put("sectionName",section.getName());
                m.put("reservation",reservation);
                data.add(m);
            }
            WebUtil.getSession().setAttribute("user",userVo);
            return new Result(200, "查询成功", data);
        }
        return new Result(500,"查询失败");
    }

    @Override
    public Result selectByPrimaryKey(int id) {
        Reservation reservation = reservationMapper.selectByPrimaryKey(id);
        return new Result(200,"查询成功",reservation) ;
    }

    @Override
    public DiseaseExcel queryByReservationIdAndDown(int id) {
        DiseaseExcel diseaseExcel = reservationMapper.queryByReservationIdAndDown(id);
        return diseaseExcel;
    }


}
